﻿Public Class functionPopUp

    Public Func As sqlTable
    Dim sqlbuilder As sqlBuilder

    Public Sub New(ByRef func As sqlTable, ByRef sqlbuilder As sqlBuilder)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Name = func.tableName
        Me.sqlbuilder = sqlbuilder
        Me.Label1.Text = func.ToString


        Dim fll As New List(Of Object) ' this is the list of lists

        For Each pl As functionParameters In func.parametersLists
            'add a choice (at least one) for each overload version
            Dim fl As New List(Of String)  ' this is the list of options for one parameter


            Dim paramNumber As Integer = 0
            For Each fp As functionParameter In pl
                paramNumber = paramNumber + 1
                'look for it in the available tables
                If fp.drawFromAField Then
                    'check if the field is available
                    For Each f As drawFromField In fp.drawFromFieldList
                        Dim sl As List(Of String) = sqlbuilder.findFieldInTables(f.fieldname)

                        If sl.Count = 0 Then
                            fl.Add(" ?-" & f.fieldname & " ")

                        Else

                            For Each s As String In sl
                                'add a new line!

                                fl.Add(s)
                            Next

                        End If
                    Next


                Else ' not draw from a field!

                    fl.Add(" ?-" & fp.type.ToString & " ")



                End If


                fll.Add(fl)
            Next


            'create a line from the param lists

            For Each s As String In fll.Item(0)
                Dim sb As New System.Text.StringBuilder()
                sb.Append(s)
                If fll.Count > 1 Then buildComboBoxList(sb, fll, 1)
                Me.functionComboBox.Items.Add(func.tableName & "(" & sb.ToString & ")")

            Next
        Next
        Me.functionComboBox.SelectedIndex = 0


    End Sub


    Private Sub buildComboBoxList(ByRef sb As System.Text.StringBuilder, ByRef fll As List(Of Object), ByVal index As Integer)
        For Each s As String In fll.Item(index)
            sb.Append("," & s)
            If fll.Count > index + 1 Then buildComboBoxList(sb, fll, index + 1)
        Next

    End Sub




    Private Sub addItemButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub




    Private Sub cancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class